<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>经纪人</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">
            <div class="layui-btn-group">
                <button class="layui-btn layui-btn-sm" id="add_agent_id">添加经纪人</button>
            </div>
        </div>
        <div class="layui-card-body">
            <table class="layui-hide" id="agent_table" lay-filter="agent_table"></table>
        </div>

    </div>
</div>
</div>


<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="addAgent">添加经纪人</button>
        <!--<button class="layui-btn layui-btn-sm" lay-event="ModifyAgent">修改经纪人</button>-->
    </div>
</script>

<script type="text/html" id="bar">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-xs layui-btn-normal" title="认证用户" lay-event="certification"
                th:text="认证"></button>
        <button class="layui-btn layui-btn-xs layui-btn-normal" title="推荐的客户" lay-event="electCustomers">
            <i class="layui-icon">&#xe6c6;</i>
        </button>
        <button class="layui-btn layui-btn-xs layui-btn-danger" title="删除用户" lay-event="del">
            <i class="layui-icon">&#xe640;</i>
        </button>
    </div>
</script>
<script src="/layui/layui.js"></script>
<script>
    //一般直接写在一个js文件中
    layui.use(['layer', 'form', 'table'], function () {
        var layer = layui.layer, form = layui.form, table = layui.table, $ = layui.$;

        function isObjectEmpty(obj) {
            for (var key in obj) {
                if (key) {
                    return false
                }
            }
            return true
        }

        /**
         * cookie操作
         */
        function setCookie(name, value, iDay) {
            var oDate = new Date();
            oDate.setDate(oDate.getDate() + iDay);
            document.cookie = name + '=' + value + ';expires=' + oDate;
        }

        function getCookie(name) {
            var arr = document.cookie.split('; ');
            for (var i = 0; i < arr.length; i++) {
                var arr2 = arr[i].split('=');
                if (trim(arr2[0]) === name) {
                    return unescape(trim(arr2[1]));
                }
            }
            return '';
        }

        function removeCookie(name) {
            setCookie(name, 1, -1);
        }

        function trim(str) {
            return str.replace(/(^\s*)|(\s*$)/g, "");
        }

        Date.prototype.Format = function (fmt) {
            var o = {
                "M+": this.getMonth() + 1,//月份
                "d+": this.getDate(),//日
                "H+": this.getHours(),//小时
                "m+": this.getMinutes(),//分
                "s+": this.getSeconds(),//秒
                "q+": Math.floor((this.getMonth() + 3) / 3),//季度
                "S+": this.getMilliseconds()//毫毛
            };
            if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in o)
                if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return fmt;
        };

        $('#add_agent_id').click(function (e) {
            addAgent();
        })
        table.render({
            elem: '#agent_table'
            , url: 'agentinfo/' + getCookie("telphone")
            , cellMinWidth: 80     //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            , cols: [
                [
                    {
                        title: '序号', templet: function (d) {
                            return d.LAY_TABLE_INDEX + 1
                        }, align: 'center'
                    },
                    {field: 'agentName', title: '经纪人昵称', align: 'center'}
                    , {field: 'type', title: '经纪人类型', align: 'center'}
                    , {field: 'telphone', title: '电话', sort: true, align: 'center'}
                    , {
                    field: 'superiorName', title: '上级经纪人', templet: function (d) {
                        if (d.superiorName == '') {
                            return "无";
                        } else {
                            return d.superiorName;
                        }
                    }, sort: true, align: 'center'
                }
                    , {
                    field: 'bcertification', title: '是否实名认证', align: 'center', templet: function (d) {
                        if (d.bcertification === undefined) {
                            return "未认证";
                        } else if (d.bcertification === 0) {
                            return "未认证";
                        } else if (d.bcertification === 1) {
                            return "已认证";
                        } else if (d.bcertification === 2) {
                            return "等待认证";
                        }
                    }
                }
                    , {
                    field: 'regDatetime', title: '注册时间', templet: function (d) {
                        return new Date(new Date(d.regDatetime).toString()).Format("yyyy-MM-dd");
                    }, align: 'center'
                }
                    , {
                    field: 'reconCustomNam', title: '邀请客户数量', sort: true, align: 'center'
                }, {
                    field: 'forbid', title: '状态', align: 'center', templet: function (d) {
                        if (d.forbid == undefined) {
                            return "<div class='layui-text'>有效</div>"
                        } else if (d.forbid == 1) {
                            return "<div class='layui-text'>无效</div>"
                        } else if (d.forbid == 0) {
                            return "<div class='layui-text'>有效</div>"
                        }
                    }
                }
                    , {fixed: 'right', title: '操作', toolbar: '#bar', width: 150}
                ]
            ]
            , page: true
            , done: function (res, curr, count) {
            }
        });

        //判断浏览器大小方法
        function screen() {
            //获取当前窗口的宽度
            var width = $(window).width();
            if (width > 1200) {
                return 3;   //大屏幕
            } else if (width > 992) {
                return 2;   //中屏幕
            } else if (width > 768) {
                return 1;   //小屏幕
            } else {
                return 0;   //超小屏幕
            }
        }

        /**
         * 修经纪人
         * @param obj
         */
        function modifyAgent(obj) {
            layer.open({
                type: 2,
                title: "修改经纪人信息",
                anim: 1,
                maxmin: true,
                offset: 'auto',
                content: 'modifyenter?id=' + obj[0].id,
                area: screen() < 2 ? ['90%', '80%'] : ['1000px', '680px'],
            });
        }

        /**
         * 添经纪人
         */
        function addAgent() {
            layer.open({
                type: 2,
                title: "经纪人信息",
                anim: 1,
                maxmin: true,
                offset: 'auto',
                content: 'addagent?user_id=' + getCookie("telphone"),
                area: screen() < 2 ? ['90%', '80%'] : ['1000px', '680px'],
                end: function (layero, index) {
                    table.reload('agent_table');
                }
            });
        }

        /**
         * 显示经纪人邀请的客户
         * @param obj
         */
        function showAgentCustomers(obj) {
            layer.open({
                type: 2,
                title: obj.agentName + "邀请的客户",
                anim: 1,
                maxmin: true,
                offset: 'auto',
                content: 'showCustomer?id=' + obj.id + '&companyId=' + obj.companyId,
                area: screen() < 2 ? ['90%', '60%'] : ['1000px', '580px'],
            });
        }


        //头工具栏事件
        table.on('toolbar(agent_table)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);

            switch (obj.event) {
                case 'addAgent':
                    addAgent();
                    break;
                case 'ModifyAgent':
                    var data = checkStatus.data;
                    if (isObjectEmpty(data)) {
                        return layer.msg("请先选择一位经纪人");
                    } else if (data.length > 1) {
                        return layer.msg("只能选择一位经纪人")
                    }
                    modifyAgent(data);
                    break;
                default:
                    break;
            }
            ;
        });

        //监听行工具事件
        table.on('tool(agent_table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('确定删除此用户吗?', function (index) {
                    var jsondata = {};
                    jsondata.id = data.id;
                    $.post('removeagent', jsondata, function (resp) {
                        if (resp.code === "1") {
                            layer.msg(resp.msg);
                            layer.close(index);
                            table.reload("agent_table");
                        } else {
                            layer.msg(resp.msg);
                        }
                    });
                });
            } else if (obj.event === 'certification') {
                layer.confirm('是否认证此用户?', function (index) {
                    var jsondata = {};
                    jsondata.id = data.id;
                    $.post('authuser', jsondata, function (resp) {
                        if (resp.code === "1") {
                            layer.msg(resp.msg);
                            layer.close(index);
                            table.reload("agent_table");
                        } else {
                            layer.msg(resp.msg);
                        }
                    });
                });
            } else if (obj.event === 'electCustomers') {
                showAgentCustomers(data);
            }
        });
    });
</script>
</body>
</html>